package com.nowoe.server.servlet;

import com.nowoe.server.constant.Http;
import com.nowoe.server.http.Request;
import com.nowoe.server.http.Response;
import com.nowoe.server.util.HttpProtocolUtil;

/**
 * @author 倚栏听风 2024/1/5 15:57
 */
public abstract class HttpServlet implements Servlet {
    public abstract void doGet(Request request, Response response) throws Exception;

    public abstract void doPost(Request request, Response response) throws Exception;

    @Override
    public void init() throws Exception {

    }

    @Override
    public void service(Request request, Response response) throws Exception {
        if (request.getMethod().equals(Http.GET)) {
            doGet(request, response);
        } else if (request.getMethod().equals(Http.POST)) {
            doPost(request, response);
        } else {
            response.output(HttpProtocolUtil.getHttpHeader(Http.NOT_ALLOWED, 0));
        }
    }

    @Override
    public void destroy() throws Exception {

    }
}
